<template>
  <div class="product">
    <div class="header">
      <div class="add">
        <el-button type="primary" @click="addProduct">新增</el-button>
      </div>
      <div class="form">
        <el-form>
          <el-form-item>
            <el-input v-model="keywords" placeholder="请输入商品编号或者名称" clearable>
              <el-button slot="append" icon="el-icon-search" @click="getProductsByKeyWords"/>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="table">
      <el-table :data="list" border fit>
        <el-table-column label="编号" align="center">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>

        <el-table-column label="商品名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>

        <el-table-column label="商品种类" align="center">
          <template slot-scope="scope">
            {{ scope.row.category_detail.name }}
          </template>
        </el-table-column>

        <el-table-column label="价格（元）" align="center">
          <template slot-scope="scope">
            {{ scope.row.price }}
          </template>
        </el-table-column>

        <el-table-column label="库存（件）" align="center">
          <template slot-scope="scope">
            {{ scope.row.inventory }}
          </template>
        </el-table-column>

        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.created_at }}
          </template>
        </el-table-column>

        <el-table-column label="更新时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.updated_at }}
          </template>
        </el-table-column>

        <!-- <el-table-column label="商品背景图" align="center" width="200">
          <template slot-scope="scope">
            <img :src="scope.row.image_url" width="100" height="100"/>
          </template>
        </el-table-column> -->

         <el-table-column label="商品状态" align="center">
          <template slot-scope="scope">
            {{ scope.row.status===0? '下架':'上架' }}
          </template>
        </el-table-column>

        <el-table-column label="是否设为热门">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.hot"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="200" align="center">
          <template slot-scope="scope">
            <el-button type="primary" @click="editProduct(scope.row.id)">详情</el-button>
            <el-button :loading="loading" type="warning" @click="updateStatus(scope.row)" v-if="scope.row.status===1">下架</el-button>
            <el-button :loading="loading" type="success" @click="updateStatus(scope.row)" v-if="scope.row.status===0">上架</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
    :total="total"
    :page.sync="listQuery.page"
    :limit.sync="listQuery.limit"
    @pagination="getList" />
  </div>
</template>
<script>
import productApi from '@/api/product.js'
import Pagination from '@/components/Pagination'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      keywords: '', // 搜索关键字
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      },
      loading: false // loading状态
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取全部商品
    async getList(e) {
      const query = {}
      if(e == undefined) {
        query.page = this.listQuery.page - 1,
        query.limit = this.listQuery.limit
      }
      else {
        query.page = e.page - 1,
        query.limit = e.limit
      }
      const result = await productApi.getProducts(query)
      this.list = result.data.rows
      this.total = result.data.count
      result.data.rows.forEach(value => {
        if(value.hot ===0) {
          value.hot = false
        }else {
          value.hot = true
        }
      })
    },
    // 跳转新增商品
    addProduct() {
      this.$router.push({
        name: 'AddProduct'
      })
    },
    // 跳转编辑商品
    editProduct(id) {
      this.$router.push({
        name: 'EditProduct',
        params: {id: id}
      })
    },
    // 上下架操作
    async updateStatus(row) {
      const data = {
        status: 1
      }
      if(row.status===0) {
        await productApi.updateStatus(row.id, data)
        this.$message.success('上架成功')
      }else {
        data.status = 0
        await productApi.updateStatus(row.id, data)
        this.$message.success('下架成功')
      }
      this.reload()
    },
    // 根据搜索关键字获取商品
    async getProductsByKeyWords() {
      if(this.keywords) {              //搜索关键字不为空
        const query = {}
        if(Number(this.keywords)) {
          query.id = this.keywords
        }else {
          query.name = this.keywords
        }
        const result = await productApi.getProductsByKeyWords(query)
        if(result.code == 0) {
          this.list = []
          this.list = result.data
          result.data.forEach(value => {
            if(value.hot ===0) {
              value.hot = false
            }else {
              value.hot = true
            }
          })
        }
      }else {                        // 搜索关键字为空
        this.getList()
      }
    }
  },
  inject: ['reload']
}
</script>
<style lang="less" scoped>
  .product {
    margin: 30px;
    position: relative;
    .header {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
  }
</style>
